/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;

public class GestorIntentsWeb extends Activity
{
	public static boolean llamada_externa=false;
	public static String id_punto;
	public static Activity gestorintents;
	
	private boolean exit;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.intro);   
        if (!isTaskRoot()) {
            final Intent intent = getIntent();
            final String intentAction = intent.getAction(); 
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
                Log.w("MAIN", "Main Activity is not the root.  Finishing Main Activity instead of launching.");
                finish();
                return;       
            }
        }
        exit = false;
        gestorintents=this;
		 Thread introTimer = new Thread()	{
				@Override
				public void run() {
					super.run();
					try	{
						sleep(2000);
						if (!exit)
							//startActivity(new Intent("org.arpucp.VINICIAL"));
							//startActivity(new Intent("org.grupoavatar.guiaubicua.VINICIAL"));
						{
							Uri data = getIntent().getData();		
							if (data!=null) {														
								List<String> params = data.getPathSegments();
								if (params.size()>0) {
									String grupo = params.get(0); // grupo
									if (grupo.equalsIgnoreCase("Ubicua")) {
										if (params.size()>1) {
											String metodo = params.get(1); // metodo
											if (metodo.equalsIgnoreCase("punto")) {
												if (params.size() >2) {
													id_punto = params.get(2); // id_punto
													Log.d(VInicial.TAG + "-CargarPuntoExterno", "id_punto "+id_punto);
													cargaJSON();
												}else {
													//Mensaje 404 xq faltan parametros
													//startActivity(new Intent(GestorIntentsWeb.gestorintents,MetododNoEncontrado.class));
													startActivity(new Intent(GestorIntentsWeb.gestorintents,VInicial.class));
												}
											}
											else {
											//Mensaje 404 xq punto es el unico metodo soportado de momento
											//startActivity(new Intent(GestorIntentsWeb.gestorintents,MetododNoEncontrado.class));
											startActivity(new Intent(GestorIntentsWeb.gestorintents,VInicial.class));
											}
										} else {
										//Mensaje 404 xq faltan parametros	
										//startActivity(new Intent(GestorIntentsWeb.gestorintents,MetododNoEncontrado.class));
										startActivity(new Intent(GestorIntentsWeb.gestorintents,VInicial.class));
										}
									}
									else {
									//Mensaje 404 xq tiene que ser UBICUA	
									//startActivity(new Intent(GestorIntentsWeb.gestorintents,MetododNoEncontrado.class));
									startActivity(new Intent(GestorIntentsWeb.gestorintents,VInicial.class));
									}
								} else {
									//Mensaje 404 xq faltan parametros		
									//startActivity(new Intent(GestorIntentsWeb.gestorintents,MetododNoEncontrado.class));
									startActivity(new Intent(GestorIntentsWeb.gestorintents,VInicial.class));
								} 	
							} else {
								//Esta entrando como siempre
								startActivity(new Intent(GestorIntentsWeb.gestorintents,VInicial.class));
							}						
						}
					}
					catch (InterruptedException e) {
						e.printStackTrace();
					}
					finally	{
						finish();
					}
					
				}
	        	
	        };
	        introTimer.start();		
	}
	
	protected void cargaJSON() {
		// TODO Auto-generated method stub
		GestorIntentsWeb.llamada_externa=true;
		Intent intent = new Intent("org.grupoavatar.guiaubicua.VINICIAL");
//		Bundle b = new Bundle();
//		b.putString("id", id_punto);
		VInicial.id = id_punto;
		id_punto = "";
//		intent.putExtras(b);
		startActivity(intent);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit = true;
			return super.onKeyDown(keyCode, event);
		}
		return false;
	}
}
